﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.NodeFactory;
using System.Data;

namespace MartinsNeves.masterpages
{
    public partial class PA_Contactos : System.Web.UI.MasterPage
    {
        public string emailTo;
        public string emailFrom;
        public string subject;
        public string successMsg;
        public string formName;

        protected void Page_Load(object sender, EventArgs e)
        {

            Node currentNode = Node.GetCurrent();
            pageTitle.Text = currentNode.GetProperty("contactTitle").Value;
            formTitle.Text = currentNode.GetProperty("contactTitleMail").Value;
            emailFrom = currentNode.GetProperty("mailFrom").Value;
            emailTo = currentNode.GetProperty("mailTo").Value;
            subject = currentNode.GetProperty("mailSubject").Value;
            successMsg = currentNode.GetProperty("mailSuccessMessage").Value;

            cleanBut.Click += new EventHandler(cleanBut_Click);
            enviarButton.Click += new EventHandler(enviarButton_Click);
        }

        void enviarButton_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
                return;


            string msg = "<p><strong>Nome:</strong> " + nameTb.Text + "</p>";
            msg += "<p><strong>Telefone:</strong> " + phoneTb.Text + "</p>";
            msg += "<p><strong>Email:</strong> " + emailTb.Text + "</p>";
            msg += "<p><strong>Mensagem:</strong> " + msgTb.Text + "</p>";

            ClassEmail.sendMail(emailFrom, emailTo, string.Empty, subject, msg);
            ClassEmail.insertContact(nameTb.Text, emailTb.Text, phoneTb.Text, msgTb.Text, formName, Request.UserHostAddress, "Ponto Alimentar", DateTime.Now);

            SucessMessage.Visible = true;
            successMsgLab.Text = successMsg;
            FormPanel.Visible = false;
        }

        void cleanBut_Click(object sender, EventArgs e)
        {
            nameTb.Text = string.Empty;
            phoneTb.Text = string.Empty;
            emailTb.Text = string.Empty;
            msgTb.Text = string.Empty;
        }
    }
}